﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Extensions;
using MvcEngine.LinqToSqlProvider;

namespace MvcEngine.Gallery.LinqToSqlProvider
{
    public class GalleryCategoryRepository : Repository, IGalleryCategoryRepository
    {
        public GalleryCategory Get(Guid id)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryCategories().SingleOrDefault(p => p.Id == id);
            }
        }

        public GalleryCategory Get(string name, Guid galleryId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryCategories().SingleOrDefault(p => p.Slug == name && p.GalleryId == galleryId);
            }
        }

        public void Add(GalleryCategory image)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.GalleryCategories().InsertOnSubmit(image);
                contextManager.Commit();
            }
        }

        public void Update(GalleryCategory image)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Commit();
            }
        }

        public void Delete(Guid id)
        {
            using (var contextManager = CreateContextManager(false))
            {
                var category = contextManager.Context.GalleryCategories().SingleOrDefault(c => c.Id == id);
                if (category != null)
                {
                    contextManager.Context.GalleryCategories().DeleteOnSubmit(category);
                    contextManager.Commit();
                }
            }
        }

        public IPagination<GalleryCategory> Get(Guid galleryId, int pageIndex, int pageSize)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryCategories().Where(p => p.GalleryId == galleryId).OrderBy(p => p.OrderNumber).GetPage(pageIndex, pageSize);
            }
        }

        public IList<GalleryCategory> GetAllByGallery(Guid galleryId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryCategories().Where(c => c.GalleryId == galleryId && c.Visible).ToList();
            }
        }

        public IList<GalleryCategory> GetAllByImage(Guid imageId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return (from ic in contextManager.Context.GalleryImageCategories()
                        join image in contextManager.Context.GalleryImages() on ic.ImageId equals image.Id
                        join cat in contextManager.Context.GalleryCategories() on ic.CategoryId equals cat.Id
                        where image.Id == imageId
                        select cat).ToList();
            }
        }
    }
}
